package com.zkk.web;

import com.alibaba.fastjson.JSON;
import com.zkk.pojo.Card;
import com.zkk.service.CardService;
import com.zkk.service.impl.CardServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/card_binding")
public class card_bindingServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            Map<String, String[]> parameterMap = req.getParameterMap();
            Card card = new Card();
            BeanUtils.populate(card,parameterMap);
            CardService cardService = new CardServiceImpl();
            int i = cardService.addCard(card);
            String s = JSON.toJSONString(i);
            resp.getWriter().write(s);


        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }
}
